package com.ruoyi.flowable.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.vo.AppVersionVo;
import com.ruoyi.flowable.domain.bo.AppVersionBo;
import com.ruoyi.flowable.service.IAppVersionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 事项版本
 *
 * @author zxw
 * @date 2023-03-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/flowable/appVersion")
public class AppVersionController extends BaseController {

    private final IAppVersionService iAppVersionService;

    /**
     * 查询事项版本列表
     */
    @SaCheckPermission("flowable:appVersion:list")
    @GetMapping("/list")
    public TableDataInfo<AppVersionVo> list(AppVersionBo bo, PageQuery pageQuery) {
        return iAppVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出事项版本列表
     */
    @SaCheckPermission("flowable:appVersion:export")
    @Log(title = "事项版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppVersionBo bo, HttpServletResponse response) {
        List<AppVersionVo> list = iAppVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "事项版本", AppVersionVo.class, response);
    }

    /**
     * 获取事项版本详细信息
     *
     * @param versionId 主键
     */
    @SaCheckPermission("flowable:appVersion:query")
    @GetMapping("/{versionId}")
    public R<AppVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long versionId) {
        return R.ok(iAppVersionService.queryById(versionId));
    }

    /**
     * 新增事项版本
     */
    @SaCheckPermission("flowable:appVersion:add")
    @Log(title = "事项版本", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(iAppVersionService.insertByBo(bo));
    }

    /**
     * 修改事项版本
     */
    @SaCheckPermission("flowable:appVersion:edit")
    @Log(title = "事项版本", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(iAppVersionService.updateByBo(bo));
    }

    /**
     * 删除事项版本
     *
     * @param versionIds 主键串
     */
    @SaCheckPermission("flowable:appVersion:remove")
    @Log(title = "事项版本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{versionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] versionIds) {
        return toAjax(iAppVersionService.deleteWithValidByIds(Arrays.asList(versionIds), true));
    }


    //部署流程
    @SaCheckPermission("flowable:appVersion:deployBpmn")
    @GetMapping(value = "/deployBpmn/{appVersionId}")
    public R<Void> deployBpmn(@PathVariable("appVersionId") Long appVersionId)
    {
        iAppVersionService.deployBpmn(appVersionId);
        return R.ok();
    }

    //复制版本
    @SaCheckPermission("flowable:appVersion:copyVersion")
    @GetMapping(value = "/copyVersion/{appVersionId}")
    public R<Void> copyVersion(@PathVariable("appVersionId") Long appVersionId)
    {
        iAppVersionService.copyVersion(appVersionId);
        return R.ok();
    }

    //设置主版本
    @SaCheckPermission("flowable:appVersion:setUseVersion")
    @GetMapping("/setUseVersion/{appVersionId}")
    public R<Void> setUseVersion(@PathVariable("appVersionId") Long appVersionId) {
        iAppVersionService.setUseVersion(appVersionId);
        return R.ok("设置成功");
    }
}
